package com.distribute.application.api.rest.member;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javassist.expr.NewArray;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.distribute.application.api.BaseApiController;
import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.title;
import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.model.Commodity;
import com.distribute.framework.model.Coupon;
import com.distribute.framework.model.Member;
import com.distribute.framework.model.PackRecord;
import com.distribute.framework.model.Product;
import com.distribute.framework.model.RedPacket;
@RestController(value="api.MemberRedController")
public class MemberRedController extends BaseApiController{
	/**
	 * 会员查看红包领取记录
	 * @return
	 */
	@RequestMapping("/api/memberRed/getRedRecordsLists.do")
	public @title("会员查看红包领取记录") Protocol getRedRecordsLists(
			@title("品牌编号") Long banrdId,
			@title("页码") Integer pageNo
			,@title("个数") Integer pageSize
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setPage(RedPacket.where.merchant(banrdId).redEnveloper(member.getId()).desc("add_time").page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<RedPacket>(){
			@Override
			public Map format(RedPacket t) {
				Map json = t.toJSON();
				Product product=Product.where.id(t.getProduct()).get();
				json.put("obj", product!=null?product.toJSON():null);
				json.put("cont", PackRecord.where.redEnvelopes(t.getId()).count());
				json.put("goods", Commodity.where.id(product.getCommodity()).get().toJSON());
				json.put("stat", t.getQuantity()==0||new Date().getTime()>t.getValidity().getTime()?1:0);
				List<PackRecord>  records=PackRecord.where.redEnvelopes(t.getId()).list(5);
				List<Member> list=new ArrayList<Member>();
				    if (records.size()>0) {
						for (PackRecord packRecord : records) {
							list.add(Member.where.uid(packRecord.getReceiver()).get());
						}
					}
				json.put("child", list);
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 会员查看红包领取下所有用户
	 * @return
	 */
	@RequestMapping("/api/memberRed/getRedRecordsUserLists.do")
	public @title("会员查看红包领取下所有用户") Protocol getRedRecordsUserLists(
			@title("红包ID") Long redId,
			@title("页码") Integer pageNo
			,@title("个数") Integer pageSize
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setPage(PackRecord.where.redEnvelopes(redId).page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<PackRecord>(){
			@Override
			public Map format(PackRecord t) {
				Map json = t.toJSON();
				json.put("user", Member.where.uid(t.getReceiver()).get().toJSON());
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 会员查看红包领取下所有用户
	 * @return
	 */
	@RequestMapping("/api/memberRed/cancelRed.do")
	public @title("会员查看红包领取下所有用户") Protocol cancelRed(
			@title("红包ID") Long redId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
			RedPacket redPacket2=RedPacket.where.id(redId).get();
			if (!member.getId().toString().equals(redPacket2.getRedEnveloper().toString())) {
				 protocol.set("msg", "请不要非法操作");
				 protocol.set("type", false);
				 return protocol;
			}
			 Integer count=redPacket2.getQuantity();
			if (count>0) {
				int ad=RedPacket.where.definex("quantity > 0").set.quantity(0).update();
				if (ad==1) {
					 Product product=Product.where.id(redPacket2.getProduct()).get();
					 product.setNumber(product.getNumber()+count);
					 product.update();
					 protocol.set("msg", "取消成功  余额已退至您的帐户");
					 protocol.set("type", true);
				}else {
					 protocol.set("msg", "取消失败");
					 protocol.set("type", false);
				}
			}else {
				 protocol.set("msg", "红包已被领取完了");
				 protocol.set("type", false);
			}
		return protocol;
	}
	
}
